<template>
<div>
  <a-modal
    title="设备列表"
    width="30%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!-- <a-checkbox v-for="item in dataList" :key="item.id" v-model:checked="item.selected" @change="checkchange">{{item.name}}</a-checkbox> -->
      <a-table :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :dataSource="dataList" >
         <a-table-column key="name" title="设备名称" data-index="name" />
        <a-table-column key="number" title="设备编号" data-index="number" />
        <a-table-column key="deviceType" title="设备类型" data-index="deviceType">
          <template slot-scope="text, record">
            <span v-if="record.deviceType == 1">音响</span>
            <span v-if="record.deviceType == 2">考勤</span>
            <span v-if="record.deviceType == 3">晨会</span>
          </template>
        </a-table-column>
      </a-table>
    </a-spin>
  </a-modal>
      <add-family ref="addFamily" @ok="handleOk" />
  </div>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
  import { childrenAdd, childrenPage } from '@/api/modular/children/childManage'
  import { parentPage } from '@/api/modular/parent/parentManage'
  import {getdeviceList, tongbuData} from '@/api/modular/garden/sound'
  import addFamily from './addFamily'
  import moment from 'moment';
  export default {
    components: {
      XDown,
      XCard,
      STable,
      addFamily,
    },
    props: {
      
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        dateFormat: 'YYYY-MM-DD',
        dataList: [],
        selectedRowKeys: [],
        
      columns: [
        {
          title: '设备名称',
          dataIndex: 'name',
          key: 'name'
        },
        {
          title: '设备编号',
          dataIndex: 'number',
          key: 'number'
        },
        {
          title: '设备类型',
          dataIndex: 'deviceType',
          key: 'deviceType',
          scopedSlots: { customRender: 'action' }
        },
      ],
      syncDataType: 0,
      users: [],
      }
    },
    created(){
      
    },
    methods: {
      // 初始化方法
      add (records, type) {
        this.users = records
        this.visible = true;
        this.syncDataType = type
        this.getList()
      },
      getList(){
        getdeviceList().then((res) => {
          if(res.code == 200){
            this.dataList = res.data.map(item => {item.selected = false; return item})
          }
          
        })
      },
      onSelectChange(rows){
        console.log(rows)
        this.selectedRowKeys = rows;
        console.log(this.selectedRowKeys)
      },
      checkchange(val){
        
      },
      handleSubmit () {
        this.confirmLoading = true
        let ids = []
        this.selectedRowKeys.forEach(idx => {
          ids.push(this.dataList[idx].id)
        })
        let userIds = this.users.map(item => {return item.id})
        let postData = {
          deviceIds: ids,
          syncDataType: this.syncDataType,
          userIds: userIds
        }
        tongbuData(postData).then((res) => {
              if (res.success) {
                this.$message.success('同步成功')
                this.visible = false
                this.confirmLoading = false
                // this.$emit('ok', values)
              } else {
                this.$message.error('同步失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
      },
      handleCancel () {
        this.visible = false
      },
      handleOk(){
        this.$refs.familytable.refresh()
        
      },

    }
  }
</script>
